package com.gugu.procedure.controller;


import com.gugu.procedure.bean.Staff;
import com.gugu.procedure.common.Constant;
import com.gugu.procedure.common.MessageResult;
import com.gugu.procedure.service.StaffService;
import com.gugu.procedure.util.DateUtil;
import com.gugu.procedure.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = {"员工模块"})
@RequestMapping("staff")
public class StaffController extends BaseController {

    @Autowired
    private StaffService staffService;


    @PostMapping("add")
    @ApiOperation(value = "新增")
    public MessageResult add(@RequestBody Staff staff) {
        return staffService.add(staff);
    }

    @PostMapping("update")
    @ApiOperation(value = "更新")
    public MessageResult update(@RequestBody Staff staff) {
        return staffService.update(staff);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public MessageResult page(String name, String mobile, String number, int pageNo, int pageSize) {
        return staffService.page(name, mobile, number, pageNo, pageSize);
    }


    @GetMapping("open/staff")
    @ApiOperation(value = "根据工号查询员工")
    public MessageResult staff(String number) {
        Staff staff = staffService.findByStaffNumber(number);
        return MessageResult.getSuccessInstance(staff);
    }


    @GetMapping("delete")
    @ApiOperation(value = "删除")
    public MessageResult delete(String number) {
        return staffService.delete(number);
    }

    @GetMapping("Import")
    @ApiOperation(value = "导入")
    public MessageResult save(String path) {
        File file = new File(pathPrefix + path);
        if (!file.exists()) {
            return MessageResult.error("文件不存在");
        }
        List<Staff> list = resolvingStaff(file);
        return staffService.saveAll(list);
    }


    List<Staff> resolvingStaff(File file) {
        List<Staff> list = new ArrayList<>();
        Workbook wb = ExcelUtil.readExcel(file);
        if (wb != null) {
            Sheet sheet = wb.getSheetAt(0);
            int index = 1;
            Row row = sheet.getRow(index);
            while (row != null) {
                Staff staff = new Staff();
                Cell cell = row.getCell(0);
                if (cell != null) {
                    String number = ExcelUtil.getCellValueByCell(cell);
                    staff.setNumber(number);
                } else {
                    continue;
                }
                cell = row.getCell(1);
                String name = ExcelUtil.getCellValueByCell(cell);
                staff.setName(name);

                cell = row.getCell(2);
                String mobile = ExcelUtil.getCellValueByCell(cell);
                staff.setMobile(mobile);

                cell = row.getCell(3);
                String dept = ExcelUtil.getCellValueByCell(cell);
                staff.setDept(dept);

                cell = row.getCell(4);
                String production = ExcelUtil.getCellValueByCell(cell);
                staff.setProduction(production);

                staff.setCreateTime(DateUtil.getFormatedDateTime());
                staff.setIsDel(Constant.IS_DEL_NO);
                list.add(staff);
                row = sheet.getRow(++index);
            }
        }
        return list;
    }
}
